<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends LoginController
{
    public function index()
    {
        $notification = D('Subscribe')->getSubNotify();
        $mostcomment = D('Subscribe')->getMostComment();
        $user = D('User')->getUser();
        $this->assign(array('user'=>$user, 'notification'=>$notification, 'mostcomment'=>$mostcomment));
        $this->display('/index/index');
    }


    public function sub(){
        $notification = D('Subscribe')->getSubNotify();
        $mostcomment = D('Subscribe')->getMostComment();
        $user = D('User')->getUser();
        $this->assign(array('user'=>$user, 'notification'=>$notification, 'mostcomment'=>$mostcomment));
        $this->display('/index/subscribe');
    }


    public function find(){
        $notification = D('Subscribe')->getSubNotify();
        $mostcomment = D('Subscribe')->getMostComment();
        $user = D('User')->getUser();
        $this->assign(array('user'=>$user, 'notification'=>$notification, 'mostcomment'=>$mostcomment));
        $this->display('/index/find');
    }


    //订阅弹出框iframe
    public function subscribeIframe()
    {
        $tags = D('Topic')->getTags();
        $this->assign('tags', $tags);
        $this->display('/index/subscribe_iframe');
    }

    //订阅
    public function subscribe(){
        $sub_tags = I('post.subscribe/a');
        $uid = session('uid');
        D("Subscribe")->subscribe($uid, $sub_tags);
        exit;
    }

    //喜欢
    public function like(){
        $nid = I('post.nid');
        $action = I('post.action');

        $data = array(
            'code' => 0,
            'message' => '操作失败',
        );

        $result = D('News')->like($nid, $action);

        if($result){
            $data['code'] = 1;
            $data['message'] = '操作成功';
        }

        $this->ajaxReturn($data, 'json');
    }


    //前端轮询
    public function message(){
        $time = I('get.time/d');
        $data = array(
            'code' => 0,
            'message' => '没有更新'
        );

        $result = D('News')->getLatestNews($time);

        if(!empty($result)){
            $data['code'] = 1;
            $data['message'] = $result;
        }
        $this->ajaxReturn($data, 'json');
    }


    //test
    public function test(){
        echo strtotime(date('Y-m-d'));
    }

}